package test;

import java.awt.Rectangle;

import org.lwjgl.input.Keyboard;

import static org.lwjgl.opengl.GL11.*;

/**
 * Class representing Player
 * 
 * @author drahomir.karchnak
 *
 * TODO:	Method for handling events
 *			Edit draw method
 *			Edit update method
 */

public class Player {
	private float x,y;
	private float prevX, prevY;
	
	private float velX, velY;
	private int movementSpeed;
	
	private Rectangle boundingBox;
	
	private static final int PLAYER_WIDTH	= 32;
	private static final int PLAYER_HEIGHT	= 64;
	private static final int PLAYER_GRAVITY = 160;
	
	/**
	 * Constructor creates player on exact position
	 * @param x
	 * @param y
	 */
	
	public Player(float x, float y, int moveSpeed){
		this.x = x;
		this.y = y;
		
		setVelocityXY(0, 0);
		setMoveSpeed(moveSpeed);
		boundingBox = new Rectangle((int)x,(int)y,PLAYER_WIDTH, PLAYER_HEIGHT);
	}
	
	/**
	 * Event Handler
	 */
	
	/*public void handleEvents(nejaka message){
	 * 	if(keyPressed(left) setVelocityX(getVelocityX() - movementSpeed );
	 *  if(keyPressed(right)setVelocityX(getVelocityX() + movementSpeed );
	 * 
	 * if(keyReleased(left) setVelocityX(getVelocityX() + movementSpeed );
	 * if(keyReleased(right) setVelocityX(getVelocityX() - movementSpeed );
	 * 
	 * a tak nejak podobne :)
	 * }
	 */
	
	/**
	 * Updating player
	 * @param deltaTime
	 */
	
	public void update(long deltaTime){
		/** This will apply gravity (player's Y velocity is increasing till end of the world **/
		setVelocityY(getVelocityY() + (PLAYER_GRAVITY * (deltaTime / 1000.0f))); 

		
		setY(getY() + (velY * (deltaTime / 1000.0f)));
		/**
		 * if(collides) backToPrevPosition();
		 */
		
		setX(getX() + (velX * (deltaTime / 1000.0f))); 
		/**
		 * if(collides) backToPrevPosition();
		 */
	}
	
	public void redraw(int cameraX, int cameraY){
		glPushMatrix();
			glTranslatef(-cameraX + getX(), -cameraY + getY(), 0);
			
			glBegin(GL_QUADS);
				/**
				 * There is space to render player
				 * 0,0 is left top corner of player
				 */
				glColor3f(1,0,0);
			
				glVertex2i(0,0);
				glVertex2i(PLAYER_WIDTH,0);
				glVertex2i(PLAYER_WIDTH,PLAYER_HEIGHT);
				glVertex2i(0,PLAYER_HEIGHT);
			glEnd();
			
		glPopMatrix();
	}
	
	/**
	 * Setting position
	 * @param x
	 */
	
	public void setX(float x){
		savePrevX();
		this.x = x;
		updateBoundingBox();
	}
	
	public void setY(float y){
		savePrevY();
		this.y = y;
		updateBoundingBox();
	}
	
	public void setXY(float x, float y){
		setX(x);
		setY(y);
		updateBoundingBox();
	}
	
	/**
	 * Getting position
	 * @return
	 */
	
	public float getX(){
		return this.x;
	}
	
	public float getY(){
		return this.y;
	}
	
	/**
	 * Saving previous Position for collision solving
	 */
	
	public void savePrevX(){
		this.prevX = x;
	}
	
	public void savePrevY(){
		this.prevY = y;
	}
	
	/**
	 * Returning to previous position
	 */
	
	public void backToPrevPosition(){
		setX(prevX);
		setY(prevY);
	}
	
	/**
	 * Setting Velocities
	 * @param velX
	 */
	
	public void setVelocityX(float velX){
		this.velX = velX;
	}
	
	public void setVelocityY(float velY){
		this.velY = velY;
	}
	
	public void setVelocityXY(float velX, float velY){
		setVelocityX(velX);
		setVelocityY(velY);
	}
	
	/**
	 * Getting velocities
	 * @return
	 */
	
	public float getVelocityX(){
		return this.velX;
	}
	
	public float getVelocityY(){
		return this.velY;
	}
	
	/**
	 * Setting movement speed of player
	 * @param speed
	 */
	
	public void setMoveSpeed(int speed){
		this.movementSpeed = speed;
	}
	
	/**
	 * Updating Bounding Box
	 */
	
	private void updateBoundingBox(){
		boundingBox.x = (int)this.x;
		boundingBox.y = (int)this.y;
	}
}